<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   提现
 */
defined('IN_YuLin') || exit('NO PERMIT!');

if(!$_uid)
{
	header('Location: '.MU('login'));
	exit;
}

$table = Table('user_money_cash');

//	是否绑卡
$bank = $db->getrow('SELECT * FROM '.Table('user_bank').' WHERE uid = '.$_uid);

if(IS_POST)
{
	if(!$bank)	//	绑定银行卡
	{
		$bank = NoBadStr($_POST['bank']);
		$bank == 'other' && $bank = NoBadStr($_POST['bankNameText']);
		
		$bankno		= NoBadStr($_POST['bankno']);
		$truename	= NoBadStr($_POST['truename']);
		$province	= NoBadStr($_POST['province']);
		$city		= NoBadStr($_POST['city']);
		$paypassword		= trim($_POST['paypassword']);
		
		(!$bank || !$bankno || !$truename || !$province || !$city || !$paypassword) && AjaxReturn(1,'请填写完整银行卡信息');

		($_Y['user']['paypassword'] != Password($paypassword)) && AjaxReturn(1,'交易密码错误');
		
		$ins = [];
		$ins['uid']		=	$_uid;
		$ins['truename']=	$truename;
		$ins['bank']	=	$bank;
		$ins['bankcard']=	$bankno;
		$ins['province']=	$province;
		$ins['city']	=	$city;
		$ins['dateline']=	TIMESTAMP;
		$db->add('user_bank',$ins);
		
		AjaxReturn(0,'绑定银行卡成功！');
	}
	
	$money = intval($_POST['money']);
	($money < 100) && AjaxReturn(1,'提现金额不能低于100元');
	
	$paypassword = trim($_POST['paypassword']);
	!$paypassword && AjaxReturn(1,'请输入交易密码');
	
	($_Y['user']['paypassword'] != Password($paypassword)) && AjaxReturn(1,'交易密码错误');
	
	($_Y['user']['money'] < $money) && AjaxReturn(1,'账户余额不足');
	
	//	更新用户的资金
	$db->exec('UPDATE '.Table('user').' SET money = money - '.$money.' WHERE id = '.$_uid);
	
	//	资金日志
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['fee']		=	-$money;
	$ins['money']	=	$_Y['user']['money'] - $money;
	$ins['notes']	=	'用户提现';
	$ins['tid']		=	3;
	$ins['dateline']=	TIMESTAMP;
	$db->add('user_money_log',$ins);

    //	提现记录
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['orderid']	=	date('YmdHis').rand(1000,9999);
	$ins['money']	=	$money;
	$ins['bank']	=	$bank;
	$ins['bankcard']=	$bankcard;
	$ins['truename']=	$truename;
	$ins['status']	=	0;
	$ins['dateline']=	TIMESTAMP;
	$db->add('user_money_cash',$ins);	
	
	AjaxReturn(0,'提现成功');
}

if($a == 'log')
{
	$data  = [];
	$where = ' FROM '.$table.' t WHERE t.uid = '.$_uid;

	$page = max(1,intval($_GET['page']));
	$size = 20;

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$limit = ' LIMIT '.($page - 1) * $size.','.$size;

		$url = MU($c.'/'.$a);
		$orderby = ' ORDER BY t.id DESC ';
		
		$data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
		//$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$tpl->display('wap/cash_log');
	exit;
}
else if($a == 'view')	//	详情
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('查看的信息不存在！',MU($c.'/log'));
	
	$data = $db->getrow('SELECT t.* FROM '.$table.' t WHERE t.id = '.$id.' AND t.uid = '.$_uid);
	!$data && ShowMsg('查看的信息不存在！',MU($c.'/log'));
	
	$tpl->display('wap/cash_view');
	exit;
}

if($bank)
{
	$tpl->display('wap/cash_2');
}
else
{
	$tpl->display('wap/cash');
}